package com.interview.rtc.aigc.controller;

import com.interview.rtc.aigc.common.ResponseResult;
import com.interview.rtc.aigc.dao.SessionHistoryDao;
import com.interview.rtc.aigc.dao.UserDao;
import com.interview.rtc.aigc.service.SessionHistoryService;
import com.interview.rtc.aigc.vo.MessageRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/session")
public class SessionHistoryController extends BaseController {

    @Autowired
    private SessionHistoryService sessionHistoryService;

    @GetMapping("/list")
    public ResponseResult<List<SessionHistoryDao>> getSessionList() {
        Long userId = getUserId();
        List<SessionHistoryDao> sessions = sessionHistoryService.getSessionList(userId);
        return ResponseResult.success(sessions);
    }

    @PostMapping("/create")
    public ResponseResult<SessionHistoryDao> createSession(@RequestBody SessionHistoryDao sessionHistory) {
        Long userId = getUserId();
        sessionHistory.setUserId(userId);
        SessionHistoryDao createdSession = sessionHistoryService.createSession(sessionHistory);
        return ResponseResult.success("会话创建成功", createdSession);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseResult<Void> deleteSession(@PathVariable Long id) {
        Long userId = getUserId();
        sessionHistoryService.deleteSession(id, userId);
        return ResponseResult.success("会话删除成功", null);
    }

    @PutMapping("/update")
    public ResponseResult<SessionHistoryDao> updateSession(@RequestBody SessionHistoryDao sessionHistory) {
        Long userId = getUserId();
        sessionHistory.setUserId(userId);
        SessionHistoryDao updatedSession = sessionHistoryService.updateSession(sessionHistory);
        return ResponseResult.success("会话更新成功", updatedSession);
    }

    @GetMapping("/detail/{id}")
    public ResponseResult<SessionHistoryDao> getSessionDetail(@PathVariable Long id) {
        Long userId = getUserId();
        SessionHistoryDao session = sessionHistoryService.getSessionDetailWithMessages(id, userId);
        return ResponseResult.success(session);
    }

    @PostMapping("/message/add")
    public ResponseResult<Void> addMessage(@RequestBody MessageRequest messageRequest) {
        UserDao user = getUser();
        sessionHistoryService.addMessage(messageRequest, user);
        return ResponseResult.success("消息添加成功", null);
    }
}
